;Must be in auto-execute section if I want to use the constants
#Include %A_ScriptDir%\AHKHID.ahk

; Splash text settings
Spash_Position_Y := A_ScreenHeight - 100
Spash_Colour_Text =FFFFFF
Spash_Colour_Background =000000
Splash_Font_Size =24
Splash_Time =1500 ; milliseconds

;Create GUI to receive messages
Gui, +LastFound
hGui := WinExist()

;Intercept WM_INPUT messages
WM_INPUT := 0xFF
OnMessage(WM_INPUT, "InputMsg")

;Register Remote Control with RIDEV_INPUTSINK (so that data is received even in the background)
r := HID_Register(65440, 1, hGui, RIDEV_INPUTSINK)

;Prefix loop
Loop {
    Sleep 1000
    If WinActive("ahk_class QWidget") Or WinActive("ahk_class VLC DirectX")
        sPrefix := "VLC"
    Else If WinActive("ahk_class Winamp v1.x") Or WinActive("ahk_class Winamp Video")
        sPrefix := "Winamp"
    Else If WinActive("ahk_class MediaPlayerClassicW")
       sPrefix := "MPC"
	Else If WinActive("ahk_class PowerDVD")
		sPrefix :=PowerDVD
    Else sPrefix := "Default"
}
Return


InputMsg(wParam, lParam) {
    Local devh, iKey, sLabel
    
    Critical
    
    ;Get handle of device
    devh := HID_GetInputInfo(lParam, II_DEVHANDLE)
    
    ;Check for error
    If (devh <> -1) ;Check that it is Griffin Airclick
        And (HID_GetDevInfo(devh, DI_HID_VENDORID, True) = 1917)
        And (HID_GetDevInfo(devh, DI_HID_PRODUCTID, True) = 4118) {
        
        ;Get data
        iKey := HID_GetInputData(lParam, uData)
		;Only get first key
	
		
        ;Check for error
        If (iKey <> -1) {
            
            ;Get keycode (located at the 6th byte)
            iKey := NumGet(uData, 4, "UChar")
			If (iKey = 0) {
				lastKey := 0
				repeatCounter := 0
				dontRepeat := 0
				Return
			}
			If (iKey = lastKey)
				repeatCounter := repeatCounter + 1
			
			If (dontRepeat = 1)
			{
				Return
			}
			
			
            ;Call the appropriate sub if it exists
			if (iKey = 2 or iKey = 4)
				sLabel := iKey
			else
				sLabel := sPrefix "_" iKey
			lastKey := iKey

            If IsLabel(sLabel)
                Gosub, %sLabel%
        }
    }
}


Default_1: ;Play/Pause
	Send {Media_Play_Pause}
	Display_Splash_Text("Play/Pause")
	dontRepeat := 1
Return

Default_8: ;Next
	Send {Media_Next}
	Display_Splash_Text("Next")
	dontRepeat := 1
Return

Default_16: ;Previous
	Send {Media_Prev}
	Display_Splash_Text("Previous")
	dontRepeat := 1
Return
	
2: ;Volume Up
	Send {Volume_Up 1}
	Display_Splash_Text("Volume Up")
Return
	
4: ;Volume Down
	Send {Volume_Down 1}
	Display_Splash_Text("Volume Down")
Return

VLC_16: ;Rewind
    SendInput !{Left}
Return

VLC_1: ;Pause
    SendInput {Space}
	dontRepeat := 1
Return

VLC_8: ;Forward
    SendInput !{Right}
Return

MPC_16: ;Rewind
    SendInput +{Left}
Return

MPC_8: ;FF
    SendInput +{Right}
Return

MPC_1: ;Pause
    SendInput {Space}
	dontRepeat := 1
Return

PowerDVD_16: ;Previous Chapter
    SendInput {P}
Return

PowerDVD_8: ;Next Chapter
    SendInput {N}
Return

PowerDVD_1: ;Pause
    SendInput {Space}
	dontRepeat := 1
Return

Display_Splash_Text(Splash_Text)
{
  Global
  SplashImage,, B Y%Spash_Position_Y% W500 CT%Spash_Colour_Text% CW%Spash_Colour_Background% FM%Splash_Font_Size% WM1000,, %Splash_Text%, Splash, Courier New
  SetTimer, Splash_Off, %Splash_Time%
}

Splash_Off:
  SplashImage, Off
Return
